package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, String companyId) {
        //1.设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //2.封装PageInof对象
        List<Role> list = roleDao.findAll(companyId);

        return new PageInfo<>(list);
    }

    @Override
    public void save(Role role) {
        //1.生成ID
        role.setId(UUID.randomUUID().toString());
        //2.保存数据
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) {
        //-- 角色分配权限

        //1.先删除该角色之前分配过的模块 -- 10
        roleDao.deleteRoleModuleByRoleId(roleid);

        //2.给该角色插入新的模块
        if(moduleIds!=null){
            String[] moduleId=moduleIds.split(",");
            for (int i = 0; i < moduleId.length; i++) {
                roleDao.saveRoleMoudle(roleid,moduleId[i]);
            }
        }

    }
    //角色查询所有
    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }
    //3.根据用户ID查询分配过的角色列表
    @Override
    public List<Role> findUserRoleByUserId(String id) {
        return roleDao.findUserRoleByUserId(id);
    }
}
